// pages/postDetail/postDetail.js
import {localData} from '../../data/data.js'
console.log(localData)
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlaying:false,
    _pid :null,
    _audioManager:null,

  },

  //根据pid取数组元素
  getPageDataByPid(pId){
    let tempData = {}
    for(var element of localData){
      if(element.postId == pId){
        tempData = element
        break
      }
    }
    return tempData
  },

  //判断当前页面的音乐是否在播放
  currentMusicPlaying(){
    return app.gIsPlayingMusic && app.gIsPlayingPostId===this.data._pid
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pId = options.pId
    this.data._pid = pId
    this.data._audioManager = wx.getBackgroundAudioManager()
    //监听背景音乐播放状态
    this.data._audioManager.onPlay(this.onMusicStart)
    this.data._audioManager.onPause(this.onMusicStop)
    var tempData = this.getPageDataByPid(pId)
    console.log(tempData)
    const postCollected =  wx.getStorageSync('post_collected')
    if(postCollected !== undefined){
      tempData.isCollecion = postCollected[pId]? 1 : 0
    }
    //播放状态
    tempData.isPlaying = this.currentMusicPlaying()
    this.setData(tempData)
  },

  //点击收藏
  onClickCollection(event){
    console.log("点击了收藏")
    let postCollected = wx.getStorageSync('post_collected')
    if(!postCollected){
      //没有值创建一个新的对象
      postCollected = {}
    }
    console.log("postCollected:"+postCollected)
    //动态属性设置{3：true},key为动态pid，value为isCollecion取反
    postCollected[this.data._pid] = this.data.isCollecion!=1

    //弹框提示
    wx.showToast({
      title: this.data.isCollecion == 1?"取消收藏":"收藏成功",
      duration: 3000
    })
    //更新data
    this.setData({
      isCollecion:this.data.isCollecion!=1
    })
    console.log(this.data)
    //收藏状态写入本地持久化
    wx.setStorageSync('post_collected', postCollected)
  },

  //点击分享
  async onClickShare(event){
    const result = await wx.showActionSheet({
      itemList: ['分享到qq','分享到朋友圈','分享到'],
    })
    //处理点击回调结果 await 和async
    console.log(result)
  },

  onMusicStart(event){
    app.gIsPlayingMusic = true
    app.gIsPlayingPostId = this.data._pid

    const pageData = this.getPageDataByPid(this.data._pid)
    console.log("页面数据："+pageData)
    this.data._audioManager.src = pageData.music.url
    this.data._audioManager.title=pageData.music.title
    this.data._audioManager.coverImgUrl = pageData.music.coverImg
    this.setData(
      {
        isPlaying:true,
      }
    )
  },

  onMusicStop(event){
    //缓存状态
    app.gIsPlayingMusic = false
    app.gIsPlayingPostId = -1
    //暂停
    this.data._audioManager.stop()
    this.setData({
      isPlaying:false,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})